package com.ny.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Autowired
    private RedisTemplate<String, String> redisTemplate; // 添加RedisTemplate

    public void sendEmail(String toEmail,String code){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(toEmail);
        message.setSubject("重置验证码后");
        message.setText("验证码是："+code);
        mailSender.send(message);

        // 将验证码存储到Redis中
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(toEmail, code); // 存储验证码到Redis
    }

    public String getCode(){
        int code = (int)((Math.random()*9000)+1000);
        return String.valueOf(code);
    }
}
